﻿// ==UserScript==
// @name WME-YandexMap
// @namespace WME-YandexMap
// @description Adding links to Yandex/2Gis/Nokia/Bing Maps for WME
// @include https://*.waze.com/*editor/*
// @include https://*.waze.com/*map-editor/*
// @include https://*.waze.com/*beta_editor/*
// @include http://n.maps.yandex.ru/*
// @include http://maps.yandex.ru/*
// @include https://maps.google.com/*
// @include http://2gis.ru/*
// @match https://*.waze.com/*map-editor/*
// @match https://*.waze.com/*editor/*
// @match https://*.waze.com/*beta_editor/*
// @updateURL https://waze-yandexmap-extended-tools.googlecode.com/svn/trunk/WME_YandexMap/waze-yandexmap.user.js
// @downloadURL https://waze-yandexmap-extended-tools.googlecode.com/svn/trunk/WME_YandexMap/waze-yandexmap.user.js
// @version 2.7.3
// @author skirda
// @grant none
// ==/UserScript==


var version = "2.7.3";
var RevLookup=true;
console.log("WME-YandexMap (" + version + "): Start");

// границы проектов lon0-lat0 = лево-верх, lon1-lat1 = право-низ,

var Arr2GisCity=new Array(
	{c:'abakan',t:'Абакан',lon0:91.148493,lat0:53.900164,lon1:91.822532,lat1:53.570522},
	{c:'almaty',t:'Алматы',lon0:76.720148,lat0:43.467339,lon1:77.104243,lat1:43.109829},
	{c:'almetevsk',t:'Альметьевск',lon0:52.198749,lat0:54.949952,lon1:52.475853,lat1:54.796869},
	{c:'armawir',t:'Армавир',lon0:40.856599,lat0:45.249437,lon1:41.319762,lat1:44.721796},
	{c:'arkhangelsk',t:'Архангельск',lon0:39.462275,lat0:64.828929,lon1:41.317017,lat1:64.285363},
	{c:'astrakhan',t:'Астрахань',lon0:47.867187,lat0:46.50974,lon1:48.18167,lat1:46.242946},
	{c:'barnaul',t:'Барнаул',lon0:83.449949,lat0:53.485245,lon1:84.083776,lat1:53.160058},
	{c:'belgorod',t:'Белгород',lon0:36.424588,lat0:50.699354,lon1:36.724468,lat1:50.491576},
	{c:'biysk',t:'Бийск',lon0:84.573055,lat0:52.645225,lon1:85.488328,lat1:51.901409},
	{c:'blagoveshensk',t:'Благовещенск',lon0:127.347707,lat0:50.600674,lon1:127.761741,lat1:50.224428},
	{c:'bratsk',t:'Братск',lon0:101.320597,lat0:56.405462,lon1:102.048297,lat1:55.997912},
	{c:'bryansk',t:'Брянск',lon0:34.119591,lat0:53.430104,lon1:34.600268,lat1:53.139985},
	{c:'v_novgorod',t:'Великий Новгород',lon0:31.135553,lat0:58.682364,lon1:31.510536,lat1:58.461694},
	{c:'vladivostok',t:'Владивосток',lon0:131.562748,lat0:43.614385,lon1:132.339527,lat1:42.805235},
	{c:'vladimir',t:'Владимир',lon0:40.169351,lat0:56.466473,lon1:40.691011,lat1:56.029602},
	{c:'volgograd',t:'Волгоград',lon0:43.973242,lat0:48.925678,lon1:44.92821,lat1:48.315128},
	{c:'vologda',t:'Вологда',lon0:39.61246,lat0:59.336048,lon1:40.073782,lat1:59.151771},
	{c:'voronezh',t:'Воронеж',lon0:38.989754,lat0:51.910993,lon1:39.611678,lat1:51.475593},
	{c:'gornoaltaysk',t:'Горно-Алтайск',lon0:85.488768,lat0:52.154998,lon1:86.454212,lat1:51.250738},
	{c:'dnepropetrovsk',t:'Днепропетровск',lon0:34.736329,lat0:48.613427,lon1:35.278999,lat1:48.341643},
	{c:'donetsk',t:'Донецк',lon0:37.514653,lat0:48.188299,lon1:38.242047,lat1:47.797406},
	{c:'ekaterinburg',t:'Екатеринбург',lon0:60.236538,lat0:57.034771,lon1:60.939082,lat1:56.600044},
	{c:'ivanovo',t:'Иваново',lon0:40.801743,lat0:57.088235,lon1:41.185247,lat1:56.892642},
	{c:'izhevsk',t:'Ижевск',lon0:52.934619,lat0:57.057068,lon1:53.492953,lat1:56.668377},
	{c:'irkutsk',t:'Иркутск',lon0:103.62301,lat0:52.648075,lon1:104.966649,lat1:51.711951},
	{c:'yoshkarola',t:'Йошкар-Ола',lon0:47.575103,lat0:56.760655,lon1:48.150626,lat1:56.508362},
	{c:'kazan',t:'Казань',lon0:48.295321,lat0:55.997169,lon1:49.531335,lat1:55.568003},
	{c:'kaliningrad',t:'Калининград',lon0:19.843247,lat0:55.054121,lon1:20.659358,lat1:54.535552},
	{c:'kaluga',t:'Калуга',lon0:35.903119,lat0:54.76571,lon1:36.47395,lat1:54.323868},
	{c:'karaganda',t:'Караганда',lon0:72.858651,lat0:50.132622,lon1:73.409223,lat1:49.649931},
	{c:'kemerovo',t:'Кемерово',lon0:85.943609,lat0:55.534059,lon1:86.262256,lat1:55.262515},
	{c:'kirov',t:'Киров',lon0:49.035362,lat0:58.797004,lon1:49.946385,lat1:58.400486},
	{c:'komsomolsk',t:'Комсомольск-на-Амуре',lon0:136.771299,lat0:50.721413,lon1:137.257084,lat1:50.189817},
	{c:'kostroma',t:'Кострома',lon0:40.729746,lat0:57.921793,lon1:41.14355,lat1:57.678341},
	{c:'krasnodar',t:'Краснодар',lon0:38.652738,lat0:45.264319,lon1:39.375902,lat1:44.944625},
	{c:'krasnoyarsk',t:'Красноярск',lon0:92.131003,lat0:56.306914,lon1:93.595133,lat1:55.812773},
	{c:'kurgan',t:'Курган',lon0:65.153256,lat0:55.530273,lon1:65.48145,lat1:55.368658},
	{c:'kursk',t:'Курск',lon0:35.892024,lat0:51.841854,lon1:36.432691,lat1:51.557738},
	{c:'lenkuz',t:'Ленинск-Кузнецкий',lon0:85.748699,lat0:54.774251,lon1:86.501493,lat1:54.179407},
	{c:'lipetsk',t:'Липецк',lon0:39.377135,lat0:52.718921,lon1:39.820705,lat1:52.491466},
	{c:'magnitogorsk',t:'Магнитогорск',lon0:58.521718,lat0:53.623234,lon1:59.502817,lat1:53.229866},
	{c:'miass',t:'Миасс',lon0:59.5303,lat0:55.277446,lon1:60.250081,lat1:54.890992},
	{c:'moscow',t:'Москва',lon0:36.763166,lat0:56.108107,lon1:38.220866,lat1:55.105405},
	{c:'murmansk',t:'Мурманск',lon0:32.698349,lat0:69.06473,lon1:33.222506,lat1:68.761069},
	{c:'nabchelny',t:'Набережные Челны',lon0:51.710023,lat0:55.86518,lon1:52.626277,lat1:55.536676},
	{c:'nahodka',t:'Находка',lon0:132.748007,lat0:42.907327,lon1:133.036644,lat1:42.722126},
	{c:'nizhnevartovsk',t:'Нижневартовск',lon0:75.920854,lat0:61.190935,lon1:77.027174,lat1:60.851182},
	{c:'n_novgorod',t:'Нижний Новгород',lon0:43.301231,lat0:56.476065,lon1:44.250653,lat1:56.073596},
	{c:'ntagil',t:'Нижний Тагил',lon0:59.761784,lat0:58.091704,lon1:60.346479,lat1:57.749301},
	{c:'novokuznetsk',t:'Новокузнецк',lon0:86.509223,lat0:54.12147,lon1:87.462249,lat1:53.497556},
	{c:'novorossiysk',t:'Новороссийск',lon0:36.941617,lat0:45.220677,lon1:38.667966,lat1:44.308124},
	{c:'novosibirsk',t:'Новосибирск',lon0:82.510765,lat0:55.247648,lon1:83.392393,lat1:54.554446},
	{c:'norilsk',t:'Норильск',lon0:87.260832,lat0:69.562188,lon1:88.505768,lat1:69.24282},
	{c:'noyabrsk',t:'Ноябрьск',lon0:74.303375,lat0:63.879187,lon1:76.261961,lat1:63.018253},
	{c:'odessa',t:'Одесса',lon0:30.516309,lat0:46.655044,lon1:30.907145,lat1:46.255108},
	{c:'omsk',t:'Омск',lon0:72.887675,lat0:55.41625,lon1:73.767507,lat1:54.786527},
	{c:'orenburg',t:'Оренбург',lon0:54.924569,lat0:51.934474,lon1:55.492074,lat1:51.664988},
	{c:'orel',t:'Орёл',lon0:35.862453,lat0:53.100811,lon1:36.307015,lat1:52.841941},
	{c:'penza',t:'Пенза',lon0:44.799634,lat0:53.347629,lon1:45.354652,lat1:53.067028},
	{c:'perm',t:'Пермь',lon0:55.614632,lat0:58.24263,lon1:56.657883,lat1:57.687295},
	{c:'petrozavodsk',t:'Петрозаводск',lon0:34.097521,lat0:61.958332,lon1:34.704693,lat1:61.692627},
	{c:'p_kamchatskiy',t:'Петропавловск-Камчатский',lon0:158.192879,lat0:53.349661,lon1:159.022701,lat1:52.855709},
	{c:'pskov',t:'Псков',lon0:28.168843,lat0:57.887497,lon1:28.486188,lat1:57.728751},
	{c:'minvody',t:'Пятигорск (МКВ)',lon0:42.601023,lat0:44.303459,lon1:43.526522,lat1:43.809756},
	{c:'rostov',t:'Ростов-на-Дону',lon0:39.359282,lat0:47.36713,lon1:39.922182,lat1:47.054343},
	{c:'ryazan',t:'Рязань',lon0:39.437034,lat0:54.880683,lon1:39.98795,lat1:54.488756},
	{c:'samara',t:'Самара',lon0:49.780398,lat0:53.712492,lon1:50.5218,lat1:53.039959},
	{c:'spb',t:'Санкт-Петербург',lon0:29.413543,lat0:60.291813,lon1:31.025315,lat1:59.536028},
	{c:'saransk',t:'Саранск',lon0:44.865611,lat0:54.301903,lon1:45.510315,lat1:53.984632},
	{c:'saratov',t:'Саратов',lon0:45.734752,lat0:51.698298,lon1:46.310632,lat1:51.354685},
	{c:'smolensk',t:'Смоленск',lon0:31.759159,lat0:54.887908,lon1:32.264515,lat1:54.689285},
	{c:'sochi',t:'Сочи',lon0:38.939135,lat0:44.353733,lon1:40.485856,lat1:43.364347},
	{c:'stavropol',t:'Ставрополь',lon0:41.662292,lat0:45.267846,lon1:42.319957,lat1:44.894653},
	{c:'staroskol',t:'Старый Оскол',lon0:37.747518,lat0:51.376996,lon1:37.979511,lat1:51.247807},
	{c:'sterlitamak',t:'Стерлитамак',lon0:55.804271,lat0:53.736985,lon1:56.131425,lat1:53.302877},
	{c:'surgut',t:'Сургут',lon0:72.380354,lat0:61.408286,lon1:73.795756,lat1:60.977162},
	{c:'syktyvkar',t:'Сыктывкар',lon0:50.430121,lat0:61.92271,lon1:51.273671,lat1:61.570344},
	{c:'tambov',t:'Тамбов',lon0:41.274139,lat0:52.824738,lon1:41.596269,lat1:52.563152},
	{c:'tver',t:'Тверь',lon0:35.539365,lat0:57.014891,lon1:36.306018,lat1:56.668274},
	{c:'tobolsk',t:'Тобольск',lon0:68.02465,lat0:58.337205,lon1:68.627772,lat1:58.061143},
	{c:'togliatti',t:'Тольятти',lon0:48.960426,lat0:53.708123,lon1:49.795328,lat1:53.028213},
	{c:'tomsk',t:'Томск',lon0:84.767252,lat0:56.595693,lon1:85.245125,lat1:56.348027},
	{c:'tula',t:'Тула',lon0:37.418202,lat0:54.317121,lon1:38.417427,lat1:53.900976},
	{c:'tyumen',t:'Тюмень',lon0:65.211818,lat0:57.26989,lon1:66.689442,lat1:56.39768},
	{c:'ulanude',t:'Улан-Удэ',lon0:107.35702,lat0:52.056482,lon1:107.989383,lat1:51.691354},
	{c:'ulyanovsk',t:'Ульяновск',lon0:48.035268,lat0:54.469311,lon1:48.770267,lat1:54.10641},
	{c:'ussuriysk',t:'Уссурийск',lon0:131.747838,lat0:43.971874,lon1:132.140642,lat1:43.708082},
	{c:'ustkam',t:'Усть-Каменогорск',lon0:82.443429,lat0:50.059191,lon1:82.804309,lat1:49.857962},
	{c:'ufa',t:'Уфа',lon0:55.713687,lat0:54.923449,lon1:56.31101,lat1:54.478525},
	{c:'khabarovsk',t:'Хабаровск',lon0:134.87774,lat0:48.605492,lon1:135.254878,lat1:48.2902},
	{c:'cheboksary',t:'Чебоксары',lon0:46.993754,lat0:56.340676,lon1:47.601603,lat1:55.982206},
	{c:'chelyabinsk',t:'Челябинск',lon0:61.189643,lat0:55.318442,lon1:61.740423,lat1:54.991858},
	{c:'chita',t:'Чита',lon0:113.10627,lat0:52.184623,lon1:113.635026,lat1:51.928002},
	{c:'yuzhnosakhalinsk',t:'Южно-Сахалинск',lon0:142.455994,lat0:47.512348,lon1:142.963443,lat1:46.586553},
	{c:'yakutsk',t:'Якутск',lon0:129.529959,lat0:62.186695,lon1:129.988145,lat1:61.801651},
	{c:'yaroslavl',t:'Ярославль',lon0:39.726128,lat0:57.773598,lon1:40.0046,lat1:57.521928}
);


var Config = {};
//12+
Config['_map_WME']     = 'https://www.waze.com/editor/?env=row&zoom={{zoom}}&lat={{lat}}&lon={{lon}}';

Config['_map_2GIS']    = 'http://maps.2gis.ru/#/?history=project/{{city}}/center/{{lon}}%2C{{lat}}/zoom/{{zoom}}/state/index/';
Config['_map_NM']      = 'http://n.maps.yandex.ru/?ll={{lon}}%2C{{lat}}&z={{zoom}}&l=pmap';
Config['_map_YM']      = 'http://maps.yandex.ru/?ll={{lon}}%2C{{lat}}&z={{zoom}}&l=pmap%2Cstv';
Config['_map_Google']  = 'http://maps.google.com/?ll={{lat}}%2C{{lon}}&z={{zoom}}&t=m';
Config['_map_SC']      = 'http://mapcam.info/speedcam/?lng={{lon}}&lat={{lat}}&z={{zoom}}&t=OSM';
Config['_map_NAVITEL'] = 'http://maps.navitel.su/?zoom={{zoom}}&lat={{lat}}&lon={{lon}}';
// sty: "h" - ariel, "r" - map
Config['_map_BING']    = 'http://www.bing.com/maps/?v=2&cp={{lat}}~{{lon}}&lvl={{zoom}}&dir=0&sty=h&form=LMLTEW';
// "hybrid.day" - ariel, "normal.day" - map
Config['_map_NOKIA']   = 'http://maps.nokia.com/{{lat}},{{lon}},{{zoom}},0,0,hybrid.day';
// (llz.zoom<5?llz.zoom+11:9)
Config['_map_LI']      = 'https://www.waze.com/en-GB/livemap/?zoom={{zoom}}&lon={{lon}}&lat={{lat}}';
Config['_map_WM']      = 'http://wikimapia.org/#lang=ru&lat={{lat}}&lon={{lon}}&z={{zoom}}&m=b';
Config['_map_OSM']     = 'http://www.openstreetmap.org/#map={{zoom}}/{{lat}}/{{lon}}';
// distance=1 == 500m, 2 == 2km... 5km, 10km
Config['_map_BP']      = 'http://www.benzin-price.ru/m/index.php?lat={{lat}}&lon={{lon}}&distance=1'

var doc = null;

function dump(obj) {
	var out = "";
	if(obj && typeof(obj) == "object"){
		for (var i in obj) {
			out += i + ": " + obj[i] + "\n";
		}
	} else {
		out = obj;
	}
	return out;
}

function GetLocationType(){
	consoleLog("location.href=" + location.href);
	consoleLog("location.hostname=" + location.hostname);

	if (location.hostname == "www.waze.com" || location.hostname == "editor-beta.waze.com")
		return "waze";
	if (location.hostname == "n.maps.yandex.ru")
		return "NM";
	if (location.hostname == "maps.yandex.ru")
		return "YM";
	if (location.hostname == "maps.google.com")
		return "google";
	if (location.hostname == "2gis.ru")
		return "2gis";
	return "";
};

//https://www.waze.com/editor/?zoom=1&lat=50.1477&lon=40.40575&env=row
//http://n.maps.yandex.ru/?ll=40.40575%2C50.1477&z=13&l=pmap
//http://maps.yandex.ru/?ll=40.40575%2C50.1477&z=13&l=pmap%2Cstv
//https://maps.google.com/?ll=50.1477,40.40575&z=13&t=m
//http://2gis.ru/#!/voronezh/center/39.17581%2C51.66679/zoom/15/state/index

function getQueryString(link, name)
{
	var pos = link.indexOf( name + '=' ) + name.length + 1;
	var len = link.substr(pos).indexOf('&');
	return (len == -1)?link.substr(pos):link.substr(pos,len);
}

function GetPermalink() {
  	var wcp=document.getElementsByClassName('WazeControlPermalink');
  	for(var i=0; i < wcp.length; ++i)
  		for (var j=0; j < wcp[i].getElementsByTagName('a').length;++j)
  		{
  			var href=wcp[i].getElementsByTagName('a')[j].href;
			if (href.indexOf(".waze.com/") > 0 && href.indexOf("/editor") > 0)
				return href;
		}
	return "";
}

function GetLatLonZoomOther()
{
	var zoom, lat, lon, c;
	var locType=GetLocationType();

	if (locType == "NM")
	{
		consoleLog("locType["+locType+"] {lat="+lat+", lon="+lon+" ,zoom="+zoom+"}");
		zoom = unsafeWindow.YMaps.Layers._objects.sat._map.getZoom();
		c=unsafeWindow.YMaps.Layers._objects.sat._map.getCenter();
		lat=c.getY();
		lon=c.getX();
	}
	else if (locType == "YM")
	{
		//consoleLog("dump(unsafeWindow.ymaps="+dump(unsafeWindow.ymaps.layer));
		var ll = getQueryString(location.href, 'll').split('%2C');
		lat=ll[1];
		lon=ll[0];
		zoom = parseInt(getQueryString(location.href, 'z'));
	}
	else if (locType == "google")
	{
		var WazeControlAttribution = doc.getElementsByClassName("kd-button permalink-button right small");
		consoleLog("WazeControlAttribution[0]="+WazeControlAttribution[0]);
		var href=""+WazeControlAttribution[0];
		var ll = getQueryString(href, 'll').split(',');
		consoleLog("ll="+ll);
		lat=ll[0];
		lon=ll[1];
		zoom = parseInt(getQueryString(href, 'z'));
	}
	else if (locType == "2gis")
	{
		// http://2gis.ru/#!/voronezh/center/39.40487%2C51.621363/zoom/15/state/index
		var ll = location.href.split('/');
		for (var i=0; i < ll.length; ++i)
		{
			if (ll[i] == "center")
			{
				var ll0=ll[i+1].split("%2C");
				if (ll0.length < 2)
					ll0=ll[i+1].split(",");
				console.log(ll0.length);
				lon=ll0[0];
				lat=ll0[1];
				consoleLog(lat);
			}
			else if (ll[i] == "zoom")
			{
				zoom=parseInt(ll[i+1]);
				consoleLog(zoom);
			}
		}
	}

	zoom = zoom - 12;
	if (zoom < 0)
		zoom=0;
	//consoleLog("unsafeWindow.YMaps.Layers._objects.sat._map.getCenter()={"+typeof(c)+"}"+dump(c));
	consoleLog("locType["+locType+"] {lat="+lat+", lon="+lon+" ,zoom="+zoom+"}");
	return {
		lat: lat,
		lon: lon,
		zoom: zoom
	};
}


function GetLatLonZoom(live)
{
	var href=GetPermalink();

	var lon = parseFloat(getQueryString(href, 'lon'));
	var lat = parseFloat(getQueryString(href, 'lat'));
	var zoom = parseInt(getQueryString(href, 'zoom'));
	zoom = live ? (zoom >= 5 ? 17 : zoom + 11) : (zoom > 6 ? 19 : zoom + 12);
	return {
		lat: lat,
		lon: lon,
		zoom: zoom
	};
}

function click_OtherMaps()
{
	consoleLog("this.id={"+this.id+"}");
	var llz;
	if (this.id == '_map_WME')
		llz=GetLatLonZoomOther();
	else
		llz=GetLatLonZoom(this.id=='_map_LI');
	var city='';
	if (this.id == '_map_2GIS')
	{
		if(llz.zoom > 18)
			llz.zoom = 18;
		for (i=0; i < Arr2GisCity.length; ++i)
		{
			if (llz.lon >= Arr2GisCity[i].lon0 && llz.lon <= Arr2GisCity[i].lon1 &&
				llz.lat >= Arr2GisCity[i].lat1 && llz.lat <= Arr2GisCity[i].lat0)
			{
				city=Arr2GisCity[i].c;
				//consoleLog("city={"+city+"}");
				break;
			}
		}
		if (city == '')
			return;
	}
	else if (this.id == '_map_WM')
	{
		// Correct the offset center of maps
		// world.waze.com   lat=51.6656   lon=39.18428
		// wikimapia.org	lat=51.665954 lon=39.180679
		//llz.lon-=0.003601;
		//llz.lat+=0.000354;
	}
	else if (this.id == '_map_WME')
	{
	}
	window.open(Config[this.id].replace("{{city}}",city).replace("{{lon}}",llz.lon).replace("{{lat}}",llz.lat).replace("{{zoom}}",llz.zoom),'_url'+this.id);
}


function consoleLog(text) {
	console.log("WME-YandexMap (" + version + "): " + text);
}

function _WME_init() {
	if(document.getElementById('WME.YandexMap_' + version) != null)
	{
		return;
	}

	var nod=document.createElement("div");
	nod.setAttribute('id', 'WME.YandexMap_' + version);
	nod.setAttribute('style', 'margin-right:10px;float:left;font-size:10px;position:absolute;z-index:2003;right: 350px;');
	nod.setAttribute('unselectable', 'on');
	nod.innerHTML=
		"<a id='_map_2GIS' style='font-size: 10px' title='Open in 2GIS Map'>[2Gis]</a>&nbsp;&nbsp;"+
		"<a id='_map_NM' style='font-size: 10px' title='Open in Yandex Narod Map'>[NYM]</a>&nbsp;"+
		"<a id='_map_SC' style='font-size: 10px' title='Open in speedcam'>[SC]</a>&nbsp;"+
		"<a id='_map_WM' style='font-size: 10px' title='Open in wikimapia.org'>[WM]</a>&nbsp;"+
		"<a id='_map_BP' style='font-size: 10px' title='Open in benzin-price.ru'>[BP]</a>&nbsp;"+
		"<a id='_map_NAVITEL' style='font-size: 10px' title='Open in Navitel'>[Navi]</a>&nbsp;"+
		"<a id='_map_OSM' style='font-size: 10px' title='Open in OSM'>[OSM]</a>&nbsp;"+
		"<a id='_map_YM' style='font-size: 10px' title='Open in Yandex Map'>[YM]</a>&nbsp;"+
		"<a id='_map_Google' style='font-size: 10px' title='Open in Google Map'>[G]</a>&nbsp;"+
		"<a id='_map_BING' style='font-size: 10px' title='Open in Bing Map'>[Bing]</a>&nbsp;"+
		"<a id='_map_NOKIA' style='font-size: 10px' title='Open in Nokia Map'>[Nokia]</a>&nbsp;"+
		"<a id='_map_LI' style='font-size: 10px' title='Open in LiveMap'>[Live]</a>&nbsp;"+

		"<a href='https://waze-yandexmap-extended-tools.googlecode.com/' title='WME-YandexMap_" + version + "' style='font-size: 10px' target='_blank'>[?]</a>&nbsp;"
		;

	document.getElementsByClassName('olControlAttribution')[0].parentElement.appendChild(nod);

	document.getElementById('_map_2GIS').onclick    = click_OtherMaps;
	document.getElementById('_map_NM').onclick      = click_OtherMaps;
	document.getElementById('_map_YM').onclick      = click_OtherMaps;
	document.getElementById('_map_Google').onclick  = click_OtherMaps;
	document.getElementById('_map_SC').onclick      = click_OtherMaps;
	document.getElementById('_map_NAVITEL').onclick = click_OtherMaps;
	document.getElementById('_map_BING').onclick    = click_OtherMaps;
	document.getElementById('_map_NOKIA').onclick   = click_OtherMaps;
	document.getElementById('_map_LI').onclick      = click_OtherMaps;
	document.getElementById('_map_WM').onclick      = click_OtherMaps;
	document.getElementById('_map_OSM').onclick     = click_OtherMaps;
	document.getElementById('_map_BP').onclick     = click_OtherMaps;
};


function wmeym_FakeLoad()
{
	consoleLog("wmeym_FakeLoad()");
	if (GetLocationType() == "waze" && document.getElementsByClassName('olControlAttribution')[0] == null)
	{
		consoleLog("wait olControlAttribution... (500ms)");
		setTimeout(wmeym_FakeLoad,500);
		return;
	}

	doc=unsafeWindow.document;

	var locType=GetLocationType();
	var cls;
	switch(locType)
	{
		case "waze":
			_WME_init();
			return;
		case "NM":
			cls="b-wm-toolbar b-wm-toolbar_type_head b-wm-app__links-toolbar b-wm-app__links-toolbar_is-bem_yes i-bem b-wm-toolbar_js_inited";
			break;
		case "YM":
			cls="l-head__gr";
			break;
		case "google":
			cls="kd-button permalink-button right small";
			break;
		case "2gis":
			cls="dg-tools-item";
			break;
	};

	var WazeControlAttribution = doc.getElementsByClassName(cls);
	if (WazeControlAttribution && WazeControlAttribution.length >= 1)
	{
		consoleLog("found '" + WazeControlAttribution[0] + "'");

		var nod=document.createElement("div");
		nod.setAttribute('id', 'WME.YandexMap_' + version);
		//nod.setAttribute('class', 'WazeControlPermalink olControlNoSelect');
		nod.setAttribute('style', 'position: absolute; z-index: 1031; right: 250px;');
		nod.setAttribute('unselectable', 'on');
		nod.innerHTML=
			"<small>&nbsp;"+
			"<a id='_map_WME' style='font-size: 10px' title='Open in WME'>[WME]</a>&nbsp;&nbsp;"+
			"<a href='https://waze-yandexmap-extended-tools.googlecode.com/' title='WME-YandexMap " + version + "' style='font-size: 10px' target='_blank'>[?]</a>&nbsp;"+
			"</small>"
			;
		WazeControlAttribution[0].parentElement.insertBefore(nod, WazeControlAttribution[0]);
		document.getElementById('_map_WME').onclick     = click_OtherMaps;
	}
	else
	{
		if (GetLocationType() != "waze")
		{
			consoleLog("not found '"+cls+"', wait... (500ms)");
			setTimeout(wmeym_FakeLoad,500);
			return;
		}
	}
}


function wmeym_bootstrap()
{
	consoleLog("wmeym_bootstrap()");
	if (typeof unsafeWindow === "undefined") {
		unsafeWindow    = ( function () {
			var dummyElem = document.createElement('p');
			dummyElem.setAttribute('onclick', 'return window;');
			return dummyElem.onclick();
		}) ();
	}

	wmeym_FakeLoad();
}

wmeym_bootstrap();
